package com.tjsinfo.tjsoa.worklog.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tjsinfo.tjsoa.worklog.vo.WorkLogDefine;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogExamine;


public class WorkLogDefineDao extends HibernateBaseDao implements IWorkLogDefineDao{

	/**
	 * 查询默认人
	 * @param 缺省的工作日志
	 * @return
	 */
	public List<WorkLogExamine> selectDefineUserNoType( WorkLogDefine workLogDefine)
	{
		List<WorkLogExamine> list = null;
		Session session = getSession();
		try {
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkLogDefine work inner join fetch  work.tjsUser where work.userId = ?");
			query.setParameter(0, workLogDefine.getTjsUser().getUserId());
			list =	query.list();
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}
	/**
	 * 查询默认可以查询的上报人用类型
	 * @param 缺省的工作日志
	 * @return
	 */
	public WorkLogDefine selectDefineAndType(final WorkLogDefine workLogDefine) {
		// TODO Auto-generated method stub
		
		return (WorkLogDefine)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkLogDefine work where work.userId = ? and work.type = ?");
						query.setParameter(0, workLogDefine.getTjsUser().getUserId());
						query.setParameter(1, workLogDefine.getType());
						return query.uniqueResult();
					}
					
				});
	}

	/**
	 * 查询默认可以查询的上报人不用类型
	 * @param 缺省的工作日志
	 * @return
	 */
	public List<WorkLogExamine> selectUserNoType(final WorkLogDefine workLogDefine) {
		// TODO Auto-generated method stub
		return (List<WorkLogExamine>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkLogDefine work  where work.tjsUser.userId = ?");
						query.setParameter(0, workLogDefine.getTjsUser().getUserId());
						return query.list();
					}
					
				});
	}

	public void addDefintlist(List<WorkLogDefine> list) {
		// TODO Auto-generated method stub
		for (WorkLogDefine workLogDefine : list) {
			save(workLogDefine);
		}
	}
	
	/**
	 * 查询可以审批的人
	 * @param 缺省的工作日志
	 * @return
	 */
	public List selectDefineUser( WorkLogDefine workLogDefine)
	{
		List<WorkLogExamine> list = null;
		Session session = getSession();
		try {
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkLogDefine work  where work.userId = ?");
			query.setParameter(0, workLogDefine.getTjsUser().getUserId());
			list =	query.list();
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}
}
